package com.micro.service.config;

import org.springframework.security.core.userdetails.User;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import java.util.HashMap;
import java.util.Map;

public class MyJwtAccessTokenConverter extends JwtAccessTokenConverter {

	@Override
	public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
		String userName = authentication.getUserAuthentication().getName();
		User user = (User) authentication.getUserAuthentication().getPrincipal();
		Map<String, Object> infoMap = new HashMap<>();
		infoMap.put("userName", userName);
		infoMap.put("roles", user.getAuthorities());
		((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(infoMap);
		return super.enhance(accessToken, authentication);
	}
}
